package cn.oracle.eshop.commondity.service.impl;

import cn.oracle.eshop.commondity.dao.PropertyDAO;
import cn.oracle.eshop.commondity.domain.PropertyDO;
import cn.oracle.eshop.commondity.domain.PropertyDTO;
import cn.oracle.eshop.commondity.domain.PropertyQuery;
import cn.oracle.eshop.commondity.service.PropertyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**商品属性管理模块的service组件
 * @author wyh
 * @ClassName PropertyServiceImpl
 * @date 2022/2/10 /22:16
 */
@Service
@Slf4j
public class PropertyServiceImpl implements PropertyService {
    @Autowired
    private PropertyDAO propertyDAO;
    /**
     * 分页查询商品属性
     * @author wyh
     * @date 2022/2/10
     * @param propertyQuery  查询条件
     * @return 商品属性
     */
    @Override
    public List<PropertyDTO> listPropertiesByPage(PropertyQuery propertyQuery) {
        try {
            List<PropertyDO> propertyDOS= propertyDAO.listPropertiesByPage(propertyQuery);
            List<PropertyDTO> propertyDTOS=new ArrayList<>(propertyDOS.size());
            propertyDOS.forEach(o->propertyDTOS.add(o.clone(PropertyDTO.class)));
            return propertyDTOS;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("error:{}",e.getMessage());
        }
        return null;
    }

    @Override
    public Boolean saveProperty(PropertyDTO propertyDTO) {
        try {
            PropertyDO propertyDO=propertyDTO.clone(PropertyDO.class);
            propertyDAO.saveProperty(propertyDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("error:{}",e.getMessage());
            return false;
        }

        return true;
    }

    @Override
    public PropertyDTO getPropertyById(Long id) {
        PropertyDTO propertyDTO = null;
        try {
            PropertyDO propertyDO = propertyDAO.getPropetryById(id);
            propertyDTO = propertyDO.clone(PropertyDTO.class);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("error:{}",e.getMessage());
        }
        return propertyDTO;
    }

    @Override
    public Boolean updateProperty(PropertyDTO propertyDTO) {
        try {
            propertyDAO.updateProperty(propertyDTO.clone(PropertyDO.class));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("error:{}",e.getMessage());
            return false;
        }


        return true;
    }
}
